﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1.Uitls
{
    public class MyHttpHelper
    {

        private string baseUrl;


        /// <summary>
        /// 基础Api
        /// </summary>
        public string BaseUrl
        {
            get
            {
                return baseUrl;
            }

            set
            {
                baseUrl = value;
                MyHttpClient = new HttpClient()
                {
                    BaseAddress = new Uri(baseUrl)
                };
            }
        }


        public HttpClient MyHttpClient { get; set; }
        public MyHttpHelper()
        {

        }

        public MyHttpHelper(string baseUrl)
        {
            BaseUrl = baseUrl;
        }

        /// <summary>
        /// 序列化返回值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="api"></param>
        /// <param name="strParams"></param>
        /// <returns></returns>
        public async Task<T> JsonHttpGet<T>(string api, Dictionary<string, string> strParams)
        {
            var res = await MyHttpGet(api, strParams);
            return await res.Content.ReadFromJsonAsync<T>();
        }


        /// <summary>
        /// 序列化返回值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="api"></param>
        /// <param name="value"></param>
        /// <param name="strParams"></param>
        /// <returns></returns>
        public async Task<T> JsonHttpPost<T>(string api, object value, Dictionary<string, string> strParams)
        {
            var res = await MyHttpPost(api, value, strParams);
            return await res.Content.ReadFromJsonAsync<T>();
        }

        /// <summary>
        /// 序列化请求
        /// </summary>
        /// <param name="api"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> MyHttpGet(string api, Dictionary<string, string> param)
        {
            string paramStr = DictionaryToParams(param);
            return await MyHttpClient.GetAsync(api + paramStr);
        }

        /// <summary>
        /// 自定义的转发功能
        /// </summary>
        /// <param name="api"></param>
        /// <param name="dataname"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public Task<HttpResponseMessage> MyHttpPost(string api, object value, Dictionary<string, string> strParams)
        {
            return MyHttpClient.PostAsync(api + DictionaryToParams(strParams), new StringContent(JsonConvert.SerializeObject(value), Encoding.UTF8, "application/json"));
        }

        /// <summary>
        /// 字典转参数
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public string DictionaryToParams(Dictionary<string, string> param)
        {
            var res = "";
            if (param != null)
            {
                var list = param.ToList();
                if (list.Count != 0)
                {
                    for (var i = 0; i < list.Count; i++)
                    {

                        if (i != 0)
                        {
                            res += "&";
                        }
                        res += $"{list[i].Key}={list[i].Value}";
                    }
                    res = "?" + res;
                }

            }
            return res;
        }
    }
}
